/*
 * Copyright 2009 Jiwei Xu (http://www.xujiwei.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

provide("CanvaShopDoc");

require("LayoutContainer");

CanvaShopDoc = Class({
    Initialize : function(app) {
        this.App = app;
        this.objects = [];
        this.layouts = [];
        this.SelectedLayout = null;
        //this.SelectedLayoutIndex = 0;
        this.NewLayout();
    },

    NewLayout : function(name) {
        var layout = new LayoutContainer(this.App.Canvas, name);
        this.layouts.push(layout);
        this.SelectLayout(layout);
        return layout;
    },

    SetSize : function(width, height) {
        for (var i = 0; i < this.layouts.length; ++i) {
            this.layouts[i].SetSize(width, height);
        }
    },

    SelectLayout : function(layout) {
        // Use layout id to idetify layout
        if (typeof layout === "string") {
            layout = this.GetLayout(layout);
        }

        if (!layout) {
            return false;
        }

        // Clear selections when switch layout
        if (this.SelectedLayout) {
            if (layout != this.SelectedLayout) {
                this.SelectedLayout.IsSelected = false;
                this.SelectedLayout.NeedUpdate = true;
                this.SelectNone();
            } else {
                return true;
            }
        }


        this.SelectedLayout = layout;
        //this.SelectedLayoutIndex = i;
        this.SelectedLayout.IsSelected = true;
        this.objects = this.SelectedLayout.GetObjects();
        return true;
    },

    GetLayouts : function() {
        return this.layouts;
    },

    GetLayout : function(id) {
        if (id) {
            for (var i = 0; i < this.layouts.length; ++i) {
                if (this.layouts[i].ID === id) {
                    return this.layouts[i];
                }
            }
        }

        return null;
    },

    GetCurrentLayout : function() {
        return this.SelectedLayout;
    },

    GetObjects : function() {
        return this.objects;
    },

    GetSelectedObjects : function() {
        var ret = [];
        for (var i = 0; i < this.objects.length; ++i) {
            if (this.objects[i].IsSelected) {
                ret.push(this.objects[i]);
            }
        }

        return ret;
    },

    ObjectAt : function(p) {
        for (var i = this.objects.length - 1; i >= 0; --i) {
          if (this.objects[i].HitTest(p)) {
              return this.objects[i];
          }
        }

        return null;
    },

    GetFirstObject : function() {
        return this.objects[0] || null;
    },

    GetLastObject : function() {
        return this.objects.length > 0 ? this.objects[this.objects.length - 1] : null;
    },

    Contains : function(obj) {
        var found = false;
        for (var i = 0; i < this.objects.length; ++i) {
            if (this.objects[i] == obj) {
                found = true;
                break;
            }
        }

        return found;
    },

    AddHead : function(obj) {
        if (!this.Contains(obj)) {
            this.objects.unshift(obj);
        }
    },

    Add : function(obj) {
        if (!this.Contains(obj)) {
            this.objects.push(obj);
        }
    },

    Remove : function(obj) {
        for (var i = 0; i < this.objects.length; ++i) {
            if (this.objects[i] == obj) {
                this.objects.splice(i, 1);
                break;
            }
        }
    },

    RemoveAll : function() {
        this.objects.splice(0, this.objects.length);
    },

    Draw : function(view) {
        for (var i = 0; i < this.layouts.length; ++i) {
            this.layouts[i].Draw(view);
        }
    },

    Select : function(obj, add) {
        if (!add) {
            this.SelectNone();
        }
        obj.IsSelected = true;
    },

    Deselected : function(obj) {
        obj.IsSelected = false;
    },

    SelectNone : function() {
        for (var i = 0; i < this.objects.length; ++i) {
            this.objects[i].IsSelected = false;
        }
    },

    SelectAll : function() {
        for (var i = 0; i < this.objects.length; ++i) {
            this.objects[i].IsSelected = true;
        }
    },

    ReverseSelect : function() {
        for (var i = 0; i < this.objects.length; ++i) {
            this.objects[i].IsSelected = !this.objects[i].IsSelected;
        }
    },

    IndexOf : function(obj) {
        if (!obj) {
            return -1;
        }

        var index = -1;
        for (var i = 0; i < this.objects.length; ++i) {
            if (this.objects[i] == obj) {
                index = i;
                break;
            }
        }

        return index;
    },

    MoveObjectUp : function(obj) {
        ASSERT_VALID(this.SelectedLayout);
        if (obj) {
            this.SelectedLayout.MoveObjectUp(obj);
        }
    },

    MoveObjectTop : function(obj) {
        ASSERT_VALID(this.SelectedLayout);
        if (obj) {
            this.SelectedLayout.MoveObjectTop(obj);
        }
    },

    MoveObjectDown : function(obj) {
        ASSERT_VALID(this.SelectedLayout);
        if (obj) {
            this.SelectedLayout.MoveObjectDown(obj);
        }
    },

    MoveObjectBottom : function(obj) {
        ASSERT_VALID(this.SelectedLayout);
        if (obj) {
            this.SelectedLayout.MoveObjectBottom(obj);
        }
    }
});
